package com.nisco.dms.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.Department;
import com.nisco.dms.exception.ServiceException;
import com.nisco.dms.service.DepartmentService;
import com.nisco.dms.util.ControllerUtil;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;

/**
 * 
 * @author joey
 * 
 */
@Controller
@RequestMapping(value = "department")
public class DepartmentController {
	@Autowired
	private DepartmentService departmentService;

	private static final String CREATE = "organization/organizationCreate";
	private static final String UPDATE = "organization/organizationUpdate";
	private static final String LIST = "organization/organizationList";
	private static final String TREE = "organization/organizationTree";
	private static String  moduleListNavId = "";
	@RequiresPermissions("Department:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate() {
		return CREATE;
	}

	@RequiresPermissions("Department:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody
	String create(Department department, HttpServletRequest request) {
		department.setParent((Department) request.getSession().getAttribute("parentDepartment"));
		departmentService.save(department);

		AjaxObject ajaxObject = new AjaxObject("组织添加成功！");
		ajaxObject.setNavTabId(moduleListNavId);
		return ajaxObject.toString();
	}

	@RequiresPermissions("Department:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable String id, Map<String, Object> map) {
		Department department = departmentService.get(id);

		map.put("department", department);
		return UPDATE;
	}

	@RequiresPermissions("Department:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	String update(Department department) {
		departmentService.update(department);

		AjaxObject ajaxObject = new AjaxObject("组织修改成功！");
		ajaxObject.setNavTabId(moduleListNavId);
		return ajaxObject.toString();
	}

	@RequiresPermissions("Department:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody
	String delete(@PathVariable String id) {
		AjaxObject ajaxObject = new AjaxObject();
		try {
			departmentService.delete(id);
			ajaxObject.setMessage("组织删除成功！");
			ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_SUCCESS);
		} catch (ServiceException e) {
			ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_FAILURE);
			ajaxObject.setMessage("组织删除失败：" + e.getMessage());
		}

		ajaxObject.setCallbackType("");
		ajaxObject.setRel("jbsxBox2organization");
		return ajaxObject.toString();
	}

	@RequiresPermissions("Department:view")
	@RequestMapping(value = "/tree", method = {RequestMethod.GET,RequestMethod.POST})
	public String tree(Map<String, Object> map,HttpServletRequest request) {
		moduleListNavId = ControllerUtil.navId(moduleListNavId, (String)request.getParameter("moduleListNavId"));
		Department department = departmentService.getTree();

		map.put("department", department);
		return TREE;
	}

	@RequiresPermissions("Department:view")
	@RequestMapping(value = "/list/{parentId}", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(Page page, @PathVariable String parentId, String keywords, Map<String, Object> map, HttpServletRequest request) {
		List<Department> departments = null;
		if (StringUtils.isNotBlank(keywords)) {
			departments = departmentService.find(parentId, keywords, page);
		} else {
			departments = departmentService.find(parentId, page);
		}

		request.getSession().setAttribute("parentDepartment", departmentService.get(parentId));

		map.put("page", page);
		map.put("departments", departments);
		map.put("keywords", keywords);

		return LIST;
	}

}
